//
//  WLBaseViewController.m
//  录音转文字助手
//
//  Created by 录音转文字助手 on 2020/8/11.
//  Copyright © 2020 录音转文字助手. All rights reserved.
//
#import "WLBaseViewController.h"
#import  <objc/runtime.h>
#import "WLAppleStore.h"
#import "UIImage+Color.h"
@interface WLBaseViewController (){
    NSMutableArray *_mainViewControllers;
}


@end

@implementation WLBaseViewController
-(UIView *)nav_background_line_view{
    if (!_nav_background_line_view) {
        _nav_background_line_view = [[UIView alloc]init];
        _nav_background_line_view.backgroundColor = UIColor.whiteColor;
    }
    return _nav_background_line_view;
}
-(UIView *)nav_background_view{
    if (!_nav_background_view) {
        _nav_background_view = [[UIView alloc]init];
        _nav_background_view.backgroundColor = UIColor.whiteColor;
        [_nav_background_view addSubview:self.nav_background_line_view];
        [self.nav_background_line_view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(_nav_background_view);
            make.height.mas_offset(0.5f);
        }];
    }
    return _nav_background_view;
}

-(void)viewDidLoad{
    [super viewDidLoad];
    [self set_Title:@""];
    self.view.backgroundColor = [UIColor whiteColor];
    if ([self respondsToSelector:@selector(setTitle)]) {
        NSString *titleAttri = [self setTitle];
        [self set_Title:titleAttri];
    }
    if ([self respondsToSelector:@selector(setTitleBlack)]) {
        NSString *titleAttri = [self setTitleBlack];
        [self set_TitleBlock:titleAttri];
    }
    
    if ([self respondsToSelector:@selector(setAttrTitle)]) {
        NSMutableAttributedString *titleAttri = [self setAttrTitle];
        [self set_AttrTitle:titleAttri];
    }
    
    if ([self respondsToSelector:@selector(setTitleView)]) {
        UIView *view = [self setTitleView];
        [self set_TitleView:view];
    }
    [self leftButtonArr];
    [self rightButtonArr];
    if (![self leftButton]) {
        [self configLeftBaritemWithImage];
    }
    
    if (![self rightButton]) {
        [self configRightBaritemWithImage];
    }
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:UIColor.clearColor] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:UIColor.clearColor]];
    
    [self.view addSubview:self.nav_background_view];
    [self.nav_background_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self.view);
        make.height.mas_offset(WL_NavigateHight);
    }];
}
-(void)nav_background_color:(UIColor *)color{
    self.nav_background_view.backgroundColor = color;
    if (color == [UIColor whiteColor]) {
        self.nav_background_line_view.backgroundColor = [UIColor lightGrayColor];
    }
}

/************************************************************
 *     延迟加载
 ************************************************************/
#pragma mark --- NORMAl
-(void)set_Title:(NSString *)title{
    if (!title) {
        return;
    }
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 120, 44)];
    label.numberOfLines=0;//可能出现多行的标题
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:title];
    //给所有字符设置字体为Zapfino，字体高度为15像素
    [str addAttribute: NSFontAttributeName value: [UIFont systemFontOfSize:17]
                range: NSMakeRange(0, str.length)];
        //分段控制，最开始4个字符颜色设置成蓝色
    [str addAttribute: NSForegroundColorAttributeName value:[UIColor whiteColor] range: NSMakeRange(0, str.length)];
    //
    //    if (@available(iOS 13.0, *)) {
    ////         _nav_background_view.backgroundColor = [UIColor systemGray4Color];
    //        [str addAttribute: NSForegroundColorAttributeName value:[UIColor darkGrayColor] range: NSMakeRange(0, str.length)];
    //
    //     } else {
    //         [str addAttribute: NSForegroundColorAttributeName value:color range: NSMakeRange(0, str.length)];
    //     }
    
    [label setAttributedText:str];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(titleClick:)];
    [label addGestureRecognizer:tap];
    self.navigationItem.titleView = label;
}
-(void)set_TitleBlock:(NSString *)title{
    if (!title) {
        return;
    }
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 120, 44)];
    label.numberOfLines=0;//可能出现多行的标题
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:title];
    //给所有字符设置字体为Zapfino，字体高度为15像素
    [str addAttribute: NSFontAttributeName value: [UIFont systemFontOfSize:17]
                range: NSMakeRange(0, str.length)];
        //分段控制，最开始4个字符颜色设置成蓝色
    [str addAttribute: NSForegroundColorAttributeName value:[UIColor blackColor] range: NSMakeRange(0, str.length)];
    //
    //    if (@available(iOS 13.0, *)) {
    ////         _nav_background_view.backgroundColor = [UIColor systemGray4Color];
    //        [str addAttribute: NSForegroundColorAttributeName value:[UIColor darkGrayColor] range: NSMakeRange(0, str.length)];
    //
    //     } else {
    //         [str addAttribute: NSForegroundColorAttributeName value:color range: NSMakeRange(0, str.length)];
    //     }
    
    [label setAttributedText:str];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(titleClick:)];
    [label addGestureRecognizer:tap];
    self.navigationItem.titleView = label;
}

-(void)set_AttrTitle:(NSMutableAttributedString *)title{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 120, 44)];
    label.numberOfLines=0;//可能出现多行的标题
    [label setAttributedText:title];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(titleClick:)];
    [label addGestureRecognizer:tap];
    self.navigationItem.titleView = label;
}

-(void)set_TitleView:(UIView *)titleView{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(titleClick:)];
    [titleView addGestureRecognizer:tap];
    self.navigationItem.titleView = titleView;
}

-(void)titleClick:(UIGestureRecognizer*)Tap{
    UIView *view = Tap.view;
    if ([self respondsToSelector:@selector(title_click_event:)]) {
        [self title_click_event:view];
    }
}

#pragma mark -- left_item
-(void)configLeftBaritemWithImage{
    if ([self respondsToSelector:@selector(set_leftBarButtonItemWithImage)]) {
        UIImage *image = [[self set_leftBarButtonItemWithImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self  action:@selector(left_click:)];
        
        self.navigationItem.leftBarButtonItem = item;
    }
}
-(void)configRightBaritemWithImage{
    if ([self respondsToSelector:@selector(set_rightBarButtonItemWithImage)]) {
        UIImage *image = [[self set_rightBarButtonItemWithImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self  action:@selector(right_click:)];
        self.navigationItem.rightBarButtonItem = item;
    }
}
#pragma mark -- left_button
-(BOOL)leftButton{
    BOOL isleft =  [self respondsToSelector:@selector(set_leftButton)];
    if (isleft) {
        UIButton *leftbutton = [self set_leftButton];
        [leftbutton addTarget:self action:@selector(left_click:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:leftbutton];
        self.navigationItem.leftBarButtonItem = item;
    }
    return isleft;
}

-(BOOL)leftButtonArr{
    BOOL isleft =  [self respondsToSelector:@selector(set_leftButtonArr)];
    if (isleft) {
        NSArray *leftbutton = [self set_leftButtonArr];
        
        NSMutableArray *items = [NSMutableArray array];
        for (UIButton *btn in leftbutton) {
            [btn addTarget:self action:@selector(left_click:) forControlEvents:UIControlEventTouchUpInside];
            
            UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
            
            [items addObject:item];
        }
        
        self.navigationItem.leftBarButtonItems = items;
    }
    return isleft;
}
#pragma mark -- right_button
-(BOOL)rightButton{
    BOOL isright = [self respondsToSelector:@selector(set_rightButton)];
    if (isright) {
        UIButton *right_button = [self set_rightButton];
        [right_button addTarget:self action:@selector(right_click:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:right_button];
        self.navigationItem.rightBarButtonItem = item;
    }
    return isright;
}

-(BOOL)rightButtonArr{
    BOOL isright = [self respondsToSelector:@selector(set_rightButtonArr)];
    if (isright) {
        NSArray *right_button = [self set_rightButtonArr];
        NSMutableArray *items = [NSMutableArray array];
        
        for (UIButton *btn in right_button) {
            [btn addTarget:self action:@selector(right_click:) forControlEvents:UIControlEventTouchUpInside];
            UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
            
            [items addObject:item];
        }
        self.navigationItem.rightBarButtonItems = items;
    }
    return isright;
}
-(void)left_click:(id)sender{
    if ([self respondsToSelector:@selector(left_button_event:)]) {
        [self left_button_event:sender];
    }
}
-(void)right_click:(id)sender{
    if ([self respondsToSelector:@selector(right_button_event:)]) {
        [self right_button_event:sender];
    }
}


-(void)dealloc{
    NSLog(@"\n----------------------------%@--销毁-----------------------\n",self.class);
    //    [MBProgressHUD hideHUD];
}
@end
